const Express = require('express');
const ReactSSR = require('react-dom/server');
const Home = require('./dist/server.js').default; // 注意这里的写法这里是commonjs require 所以要加default
const Map = require('../dist/manifest.json');
const path = require('path');

const App = Express();
App.get('/', (req, res) => {
    res.write(`<html><head></head><body><div id="app">`)
    const H = ReactSSR.renderToNodeStream(Home);
    H.pipe(res, {end: false});
    H.on('end', () => {
        res.write(`</div><script src='/dist/${Map['main.js']}'></script></body></html>`)
        res.end()
    });
});

// 处理静态目录的内容
App.use('/dist', Express.static(path.resolve(__dirname, '../dist')));

// App.use((req, res, next) => {
//     if(req.url.indexOf('dist') > -1){
//         const sq = req.url.split('/');
//         return res.sendFile(path.resolve(__dirname, '../' + [sq[1],sq[2]].join('/')));
//     }
//     next();
// })


App.listen(9093, () => {
    console.log('startings');
})